<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


$groups = $DB->fetchAll("SELECT * FROM ".$CONFIG->dbprefix."iusers_groups");



if(Antz_IntelliForm::submitted('groups')){
    switch(post('iusersAction')){
        case 'delete':
            $error = false;
            $keyname = preg_replace('/[^a-zA-Z0-9_\-]/', '', post('g_delGroup'));
            $newGroup = preg_replace('/[^a-zA-Z0-9_\-]/', '', post('g_newGroup'));
            if($DB->countRows($CONFIG->dbprefix."iusers_groups", "keyname = '{$keyname}'")==0){
                $error = true;
                JFX::addFormError('g_delGroup');
            };
            if($keyname == $newGroup){
                $error = true;
                JFX::addFormError('g_newGroup', $this->Lang('users_same_group'));
            };
            if($DB->countRows($CONFIG->dbprefix."iusers_groups", "keyname = '{$newGroup}'")==0){
                $error = true;
                JFX::addFormError('g_newGroup');
            };
            if($keyname == 'public'){
                $error = true;
                JFX::addFormError('g_delGroup', $this->Lang('cant_delete_public'));
            };
            if(!$error){
                // place users into the new group
                $DB->update($CONFIG->dbprefix.'iusers_to_groups', array("group_key"=>$newGroup), "group_key = '{$keyname}'");
                $numUpdated = $DB->affectedRows();
                // delete the user group
                //if($DB->countRows($CONFIG->dbprefix.'iusers_to_groups', "group_key = '{$keyname}'")>0){
                    $DB->delete($CONFIG->dbprefix.'iusers_groups', "keyname = '{$keyname}'");
                    $DB->delete($CONFIG->dbprefix.'iusers_to_groups', "group_key = '{$keyname}'");
                    $DB->delete($CONFIG->dbprefix.'iusers_pages_to_groups', "group_key = '{$keyname}'");
                /*}else{
                    JFX::addError('Moved '.$numUpdated.' from '.$keyname.' to '.$newGroup.', but still '.
                    $DB->countRows($CONFIG->dbprefix.'iusers_to_groups', "group_key = '{$keyname}'").' users in '.
                    $newGroup);
                };*/

                // report success
                JFX::addSuccess($this->lang('group_deleted_ok'));
            };
        break;
        case 'edit':
            $error = false;
            $keyname = preg_replace('/[^a-zA-Z0-9_\-]/', '', post('g_editGroup'));
            if($DB->countRows($CONFIG->dbprefix."iusers_groups", "keyname = '{$keyname}'")==0){
                $error = true;
                JFX::addFormError('g_editGroup');
            };
            $title = post('g_editTitle');
            if(trim($title)==''){
                $error = true;
                JFX::addFormError('g_editTitle', $this->Lang('cannot_be_blank'));
            };
            if(!$error){
                // remove users to group associations
                $DB->update($CONFIG->dbprefix.'iusers_groups', array('title'=>$title), "keyname = '{$keyname}'");
                // report success
                JFX::addSuccess($this->Lang('success_group_renamed'));
            };
        break;
        case 'new':
            $error = false;
            $keyname = preg_replace('/[^a-zA-Z0-9_\-]/', '', post('g_newKeyname'));
            $title = post('g_newTitle');
            if($DB->countRows($CONFIG->dbprefix."iusers_groups", "keyname = '{$keyname}'")>0){
                $error = true;
                JFX::addFormError('g_newKeyname', $this->Lang('already_registered'));
            };
            if(!$error){
                // going to process
                $dbParams = array(
                    'keyname'=>$keyname,
                    'title' =>$title
                );
                $DB->insert($CONFIG->dbprefix.'iusers_groups', $dbParams);

                // make sure the logout and profile pages are accessible to the members while logged in
                $logoutPageid = $this->GetConfig('logout_id');
                $DB->insert($CONFIG->dbprefix.'iusers_pages_to_groups', array('page_id' => $logoutPageid, 'group_key'=>$keyname));
                $profilePageid = $this->GetConfig('profile_id');
                $DB->insert($CONFIG->dbprefix.'iusers_pages_to_groups', array('page_id' => $profilePageid, 'group_key'=>$keyname));

                JFX::addSuccess($this->Lang('success_group_created'));
            };
        break;
    };
};



// build the delete form
$delForm = new JFX_Form('groups');
$delForm->setAction($actionUrls['groups']);
$block = $delForm->add('full');
$fs = $block->add($this->Lang('delete_group'));
$fs->addInput('hidden', 'iusersAction', '', 'delete');
$del = $fs->addSelect('g_delGroup', 'Group to delete', '0');
$del->add('None', '0');
foreach($groups as $k=>$grp){
    if($grp['keyname']=='public') continue;
    $del->add($grp['title'].' ['.$grp['keyname'].']', $grp['keyname']);
};

$new = $fs->addSelect('g_newGroup', $this->Lang('new_group_for_members'), '0');
$new->add('None', '0');
foreach($groups as $k=>$grp){
    if($grp['keyname']=='public') continue;
    $new->add($grp['title'].' ['.$grp['keyname'].']', $grp['keyname']);
};
$del->addJsEvent('onchange', 'IUsers_Admin.setDelGroup(\''.$new->getId().'\', this)');
$new->addParameter('disabled', 'disabled');
$fs->addInput('submit', 'g_submitBtn', '&nbsp;', $this->Lang('submit'));

// build the edit form
$editForm = new JFX_Form('groups');
$editForm->setAction($actionUrls['groups']);
$block = $editForm->add('full');
$fs = $block->add($this->Lang('rename_group'));
$fs->addInput('hidden', 'iusersAction', '', 'edit');
$sel = $fs->addSelect('g_editGroup', $this->Lang('group_to_rename'), '0');
$sel->add('None', '0');
foreach($groups as $k=>$grp){
    $sel->add($grp['title'].' ['.$grp['keyname'].']', $grp['keyname']);
};
$fs->addInput('text', 'g_editTitle', $this->Lang('title'), '');
$fs->addInput('submit', 'g_submitBtn', '&nbsp;', 'Submit');

// make a new group
$newForm = new JFX_Form('groups');
$newForm->setAction($actionUrls['groups']);
$block = $newForm->add('full');
$fs = $block->add($this->Lang('create_group'));
$fs->addInput('hidden', 'iusersAction', '', 'new');
$fs->addInput('text', 'g_newKeyname', $this->Lang('keyname_cant_change'), '');
$fs->addInput('text', 'g_newTitle', $this->Lang('title'), '');
$fs->addInput('submit', 'g_submitBtn', '&nbsp;', $this->Lang('submit'));

$SMARTY->assign('delForm', $delForm->render());
$SMARTY->assign('editForm', $editForm->render());
$SMARTY->assign('newForm', $newForm->render());













echo $this->fetchTemplate('admin.groups.php');
